﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using EasyBuildingCMS.BLL.CoreEngine;

namespace EasyBuildingCMS.UserControls.Core.BuildingBlock
{
    public partial class BuildingBlockAddingList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BuildingBlockRepository buildingBlockRepository = new BuildingBlockRepository();
                List<BLL.CoreEngine.BuildingBlock> buildingBlocks = buildingBlockRepository.GetAcitveBuildingBlock();

                //get category
                List<string> categories = (from buildingBlock in buildingBlocks
                                          select buildingBlock.BuildingBlockCategory).Distinct().ToList();

                foreach (string category in categories)
                {
                    AccordionPane pane = new AccordionPane();
                    pane.ID = category.Trim().Replace(" ", "-") + "_AccordionPane";
                    pane.HeaderContainer.Controls.Add(new LiteralControl(category));

                    List<BLL.CoreEngine.BuildingBlock> blocks = (from block in buildingBlocks
                                                                        where block.BuildingBlockCategory==category
                                                                        select block).ToList();
                    foreach (BLL.CoreEngine.BuildingBlock buildingBlock in blocks)
                    {
                        Button buildingBlockButton = new Button();
                        buildingBlockButton.ID = "BuildingBlockButton_" + buildingBlock.BuildingBlockID;
                        buildingBlockButton.Text = buildingBlock.BuildingBlockFriendlyName;
                        buildingBlockButton.CssClass="addListButton";
                        buildingBlockButton.OnClientClick = "triggerPostBack(" + buildingBlock.BuildingBlockID + ")";
                        pane.ContentContainer.Controls.Add(buildingBlockButton);

                    }

                    this.BuildingBlockAccordion.Panes.Add(pane);

                }
  
            }

        }

        protected void HiddenButton_Click(object sender, EventArgs e)
        {
            int buildingBlockID = (int)(BLL.Helper.ToInt(HiddenFieldForBuildingBlockID.Value));
            int? pageID = BLL.Helper.ToInt(BLL.Globals.CurrentPage);
            string buildingBlockPlaceHolderName = HttpContext.Current.Request.QueryString["placeholdername"];

            BuildingBlockInstanceRepository buildingBlockInstanceRepository = new BuildingBlockInstanceRepository();
            buildingBlockInstanceRepository.InsertBuildingBlockInstance(0, pageID, "", buildingBlockID, "", buildingBlockPlaceHolderName);

            string script = "<script language=\"javascript\">window.opener.document.getElementById('hiddenforpostback" + buildingBlockPlaceHolderName + "').click();window.close();</script>";
            Parent.Page.RegisterStartupScript("clientScript", script);
        }
    }
}